/**
 * 
 */
package com.qb.mobile.manager;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qb.mobile.model.Address;
import com.qb.mobile.model.Contact;
import com.qb.mobile.model.Email;
import com.qb.mobile.model.Phone;
import com.qb.mobile.model.Phonebook;

/**
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 *         ContactsReader.java class
 */
public class ContactsReader {

	private final static String DB_NAME = "phonebook";
	private final static String TABLE_NAME = "phonebook";

	private SQLiteDatabase sampleDB = null;
	private Context context;

	public ContactsReader(Context context) {
		this.context = context;
	}

	public void getPhonebookFromDatabase(List<Contact> listOfPhonebook) {
		listOfPhonebook.clear();
		Cursor c = sampleDB.rawQuery("SELECT * FROM " + TABLE_NAME, null);
		if (c != null) {
			if (c.moveToFirst()) {
				do {
					Contact contact = new Contact();
					contact.setId(c.getString(c.getColumnIndex("Id")));
					contact.setName(c.getString(c.getColumnIndex("Name")));
					
					contact.setAddressHome(c.getString(c.getColumnIndex("AddressHome")));
					contact.setAddressWork(c.getString(c.getColumnIndex("AddressWork")));
					
					contact.setPhoneHome(c.getString(c.getColumnIndex("PhoneHome")));
					contact.setMobilePhone(c.getString(c.getColumnIndex("MobilePhone")));
					contact.setPhoneWork(c.getString(c.getColumnIndex("PhoneWork")));
					
					contact.setIdPhoto(c.getString(c.getColumnIndex("Id_Photo")));
					contact.setPhoto(c.getBlob(c.getColumnIndex("Photo")));
					
					listOfPhonebook.add(contact);
				} while (c.moveToNext());
			}
		}
	}

	@SuppressWarnings("static-access")
	public void createOrConnectDatabase() {
		sampleDB = context.openOrCreateDatabase(DB_NAME, context.MODE_PRIVATE, null);
		sampleDB.execSQL("CREATE TABLE IF NOT EXISTS "
				+ TABLE_NAME
				+ " (Id  VARCHAR primary key," 
				+ " Name VARCHAR, PhoneHome VARCHAR, MobilePhone VARCHAR, PhoneWork VARCHAR, "
				+ " AddressHome VARCHAR, AddressWork VARCHAR, " 
				+ " EmailHome VARCHAR, EmailWork VARCHAR, Id_Photo VARCHAR, Photo Blob" 
				+ ");");
	}

	public void insertPhonebookIntoDatabase(Phonebook phoneBook) {
		ContentValues initialValues = new ContentValues();
		initialValues.put("Id", phoneBook.getId());
		initialValues.put("Name", phoneBook.getName());
		
		if(phoneBook.getPhones()!=null ){
			List<Phone> listPhone = phoneBook.getPhones();
			for(int i=0;i<listPhone.size();i++){
				Phone phone = (Phone)listPhone.get(i);
				if(phone.getType() == 1)
					initialValues.put("PhoneHome", phone.getNumber());
				if(phone.getType() == 2)
					initialValues.put("MobilePhone", phone.getNumber());
				if(phone.getType() == 3)
					initialValues.put("PhoneWork", phone.getNumber());
			}
		}
		
		if(phoneBook.getAddresses()!=null ){
			List<Address> listAddress = phoneBook.getAddresses();
			for(int i=0;i<listAddress.size();i++){
				Address address = (Address)listAddress.get(i);
				if(address.getType() == 1)
					initialValues.put("AddressHome", address.toString());
				if(address.getType() == 2)
					initialValues.put("AddressWork", address.toString());
			}
		}
		if(phoneBook.getEmails()!=null ){
			List<Email> listEmail = phoneBook.getEmails();
			for(int i=0;i<listEmail.size();i++){
				Email email = (Email)listEmail.get(i);
				if(email.getType() == 1)
					initialValues.put("EmailHome", email.getAddress());
				if(email.getType() == 2)
					initialValues.put("EmailWork", email.getAddress());
			}
		}

		initialValues.put("Photo", phoneBook.getPhoto()!=null ? 
				phoneBook.getPhoto().getPhoto() !=null ? 
				phoneBook.getPhoto().getPhoto(): null : null);
		
		initialValues.put("Id_Photo", phoneBook.getPhoto()!=null ? 
				phoneBook.getPhoto().getId() !=null ? 
				phoneBook.getPhoto().getId(): null : null);
		
		sampleDB.insert(TABLE_NAME, null, initialValues);
	}

	public void deletePhonebookFromDatabase(String id) {
		sampleDB.delete(TABLE_NAME, "id=" + id, null);
	}
}
